<?php

namespace app\common\model\xiluhouse;

use think\Model;

class Base extends Model
{
    protected $need_log_table = [];
    protected $has_weigh = true;

    public function getImageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');
        return cdnurl($value,true);
    }
    public function getImageText2Attr($value, $data)
    {
        $value = $value ? $value : (isset($data['image']) ? $data['image'] : '');
        return cdnurl($value,true);
    }
    public function getBuildImageTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['build_image']) ? $data['build_image'] : '');
        return cdnurl($value,true);
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return $value ? date('Y-m-d', $value) : '';
    }
    public function getAnswertimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['answertime']) ? $data['answertime'] : '');
        return $value ? date('Y-m-d', $value) : '';
    }

    /**
     * 根据有Id修改信息 无Id 新增信息
     */
    public function editData($data)
    {
        $type = 'after_insert';
        if (isset($data['id']) && is_numeric($data['id']) && $data['id'] > 0) {
            $res = $this->allowField(true)->save($data, ['id' => $data['id']]);
            $type = 'after_update';
        } else {
            $res  = $this->allowField(true)->save($data);
        }
        if (in_array($this->table, $this->need_log_table)) {
            $this->saveEditLog($type, $data, $this->getData());
        }
        return $res;
    }
    /**
     * 
     */
    public function getListByMap($map = [], $with = [], $order = [], $field = true, $append = [], $status = true)
    {
        if ($status && !isset($map['status'])) {
            $map['status'] = 'normal';
        }
        if (empty($order)) {
            if ($this->has_weigh) {
                $order = ['weigh' => 'desc', 'id' => 'desc'];
            } else {
                $order = ['id' => 'desc'];
            }
        }
        if (count($with) > 0) {
            $object_list = $this->with($with)->where($map)->field($field)->order($order)->paginate();
        } else {
            $object_list = $this->where($map)->field($field)->order($order)->paginate();
        }

        $list = [];
        if (!empty($object_list)) {
            foreach ($object_list as $item => $value) {
                if (!empty($append)) {
                    $list[] = $value->append($append);
                } else {
                    $list[] = $value;
                }
            }
        }
        return $list;
    }
    /**
     * 
     */
    public function getDataByMap($map = [], $with = [], $order = [], $field = true, $append = [], $status = true)
    {
        if ($status && !isset($map['status'])) {
            $map['status'] = 'normal';
        }
        if (empty($order)) {
            if ($this->has_weigh) {
                $order = ['weigh' => 'desc', 'id' => 'desc'];
            } else {
                $order = ['id' => 'desc'];
            }
        }
        if (count($with) > 0) {
            $object_list = $this->with($with)->where($map)->field($field)->order($order)->select();
        } else {
            $object_list = $this->where($map)->field($field)->order($order)->select();
        }

        $list = [];
        if (!empty($object_list)) {
            foreach ($object_list as $item => $value) {
                if (!empty($append)) {
                    $list[] = $value->append($append)->toArray();
                } else {
                    $list[] = $value->toArray();
                }
            }
        }
        return $list;
    }
    /**
     * 通过查询条件获取单条数据(数组)
     */
    public function getInfoByMap($map = [], $with = [], $field = true, $append = [], $status = true)
    {
        if ($status && !isset($map['status'])) {
            $map['status'] = 'normal';
        }
        if (count($with) > 0) {
            $object = $this->with($with)->where($map)->field($field)->find();
        } else {
            $object = $this->where($map)->field($field)->find();
        }

        if (!empty($object) && !empty($append)) {
            $return = $object->append($append);
        } else {
            $return = $object;
        }
        return empty($return) ? [] : $return->toArray();
    }
    /**
     * 保存修改信息
     */
    protected function saveEditLog($type, $original_data, $save_data)
    {
        if (empty($save_data)) {
            return false;
        }
        $adminInfo = \think\Session::get("admin");
        $admin_id = $adminInfo ? $adminInfo['id'] : 0;
        $user_id = 0;
        $request = \think\Request::instance();
        $token = $request->server('HTTP_TOKEN', $request->request('token', \think\Cookie::get('token')));
        if ($token) {
            $auth = \app\common\library\Auth::instance();
            $auth->init($token);
            if ($auth) {
                $user_id = $auth->id;
            }
        }
        $log_data = [
            "user_id" => $user_id,
            "admin_id" => $admin_id,
            "model_name" => $this->table,
            "param" => json_encode($original_data),
            "content" => json_encode($save_data),
            "url" => $request->url(),
            "ip" => $request->ip(),
            "type" => $type,
        ];
        DatabaseLog::create($log_data);
    }
}
